import moment from 'moment';
import { TRMessage } from '#/components';
import styles from './index.less';

/* 表格中点默认样式 */
const styleObj = {
  textAlign: 'center',
  display: 'inline-block',
};
/* 固定值 预测源枚举 */
export const RADIOCHECK = {
  DATASOURCE: 'dataSource',
  FIXATION: 'fixation',
};
/* 最大值 最小值 固定值 比例 枚举 */
export const DATATYPE = {
  RATEVALUE: 'rateValue',
  FIXEDVALUE: 'fixedValue',
  MAXVALUE: 'maxValue',
  MINVALUE: 'minValue',
};
/* 调整比例配置枚举 */
export const baseEcharts = [
  { name: '调整比例(%)', color: '#18A0FB', key: DATATYPE['RATEVALUE'] },
  { name: '最大(MW)', color: '#F67E26', key: DATATYPE['MAXVALUE'] },
  { name: '最小(MW)', color: '#56CCF2', key: DATATYPE['MINVALUE'] },
  { name: '固定(MW)', color: '#27AE60', key: DATATYPE['FIXEDVALUE'] },
];

/* 删除icon */
export const iconUrl = '//at.alicdn.com/t/font_1635810_54zp3na6k9q.js';
/* 删除表格 */
export const deleteTable = (id = '', data = []) => {
  const _data = data.filter((item) => {
    return item.id !== id;
  });
  return _data;
};
/* 调整比例表格样式 */
export const tableStyle = {
  '--cell-padding': 0,
  '--cell-border-vertical': '1px solid #4f4f4f',
  '--header-cell-border-vertical': '1px solid #4f4f4f',
  '--line-height': '32px',
  '--row-height': '32px',
  height: '440px',
  overflow: 'auto',
};
/* 调整比例表格配置 */
export const shortColumns = [
  {
    title: (
      <div style={{ width: '100%', display: 'flex' }}>
        <span style={{ width: '30%', ...styleObj }}>序号</span>{' '}
        <span style={{ width: '70%', ...styleObj }}>时间段</span>
      </div>
    ),
    key: 'first',
    width: '130px',
  },
  {
    title: (
      <div>
        <span style={{ ...styleObj, width: '40%' }}>比例(%)</span>
        <span style={{ ...styleObj, width: '30%' }}>最大(MW)</span>
        <span style={{ ...styleObj, width: '30%' }}>最小(MW)</span>
      </div>
    ),
    key: 'second',
    width: '235px',
  },
  {
    title: <span style={{ ...styleObj, width: '110px' }}>固定(MW)</span>,
    key: 'third',
    width: '100px',
    align: 'center',
  },
  { title: '', width: '25px', key: 'forth', align: 'center' },
];
/* 自定义生成96点 */
const timeFormat = 'HHmm';
export const TIME_ENUM = timerFn();
function timerFn() {
  const dateList = [];
  let time = moment().startOf('date');
  _.forEach(new Array(96), () => {
    time.add(15, 'minutes');
    const tK = time.format(timeFormat) === '0000' ? '2400' : time.format(timeFormat);
    dateList.push(tK);
  });
  return dateList;
}

export const checkTable = (id = '', data = [], value = '') => {
  const _data = data.map((item) => {
    if (id === item.id) {
      return {
        ...item,
        useType: value,
      };
    }
    return {
      ...item,
    };
  });
  return _data;
};
/* 初始化表格 */
export const initData = (data = []) => {
  const _data = data.map((item, index) => {
    return {
      ...item,
      _index: index * 1 + 1,
      beginIndex: TIME_ENUM?.[item?.beginIndex] || '',
      endIndex: TIME_ENUM?.[item?.endIndex] || '',
    };
  });
  return _data;
};
/* 生成表格columns */
export const comlumnsRender = (render = () => {}) => {
  let _columnsArr = [...shortColumns];
  let columns = _columnsArr.map((item, index) => {
    return {
      ...item,
      dataIndex: item?.key || '',
      render: (val, allVal) => render(val, allVal, item?.key, index + 1),
    };
  });
  return columns;
};
export const TIMEOBJ = {
  BEGIN: 'beginIndex',
  END: 'endIndex',
};

/* 更改时间 */
export const changeTime = (e, allVal, key, data) => {
  let reg = /^[0-9]{0,4}$/;
  if (!reg.test(e.target.value)) {
    TRMessage.error('请输入正确的时间段');
    return data;
  }
  const _data = data.map((item) => {
    if (item.id === allVal.id) {
      return {
        ...item,
        [key]: e.target.value,
      };
    }
    return { ...item };
  });
  return _data;
};
/* 时间段覆盖逻辑 */
export const generaTime = (e, allVal, key, data) => {
  let arr = data;
  let isReveal = Number(allVal.beginIndex) > Number(allVal.endIndex);
  let beginIndex = isReveal ? allVal.endIndex : allVal.beginIndex;
  let endIndex = isReveal ? allVal.beginIndex : allVal.endIndex;
  let startIndex = TIME_ENUM.findIndex((item) => item == beginIndex);
  let endindex = TIME_ENUM.findIndex((item) => item == endIndex);
  if (e.length < 4 || TIME_ENUM.findIndex((item) => item == e) == -1) {
    arr = arr.map((item) => {
      if (item.id == allVal.id) {
        return {
          ...item,
          [key]: '',
        };
      }
      return { ...item };
    });
    TRMessage.error('您输入的时间段不符合规范');
    return arr;
  }
  if (!(allVal.beginIndex.length && allVal.endIndex.length)) {
    return data;
  }
  arr = arr.map((item) => {
    if (!(item.beginIndex.length && item.endIndex.length)) {
      return { ...item };
    }
    if (allVal.id === item.id) {
      return {
        ...item,
        beginIndex,
        endIndex,
      };
    } else {
      if (item.beginIndex >= beginIndex && item.endIndex <= endIndex) {
        //时间段完全覆盖
        return null;
      } else if (
        (beginIndex <= item.endIndex && endIndex > item.endIndex) ||
        (beginIndex <= item.endIndex && endIndex <= item.endIndex && beginIndex > item.beginIndex)
      ) {
        //item.beginIndex < beginIndex && item.endIndex <= beginIndex &&item.endIndex < endIndex
        /*  (1300->1145)-1330   1100-(1200->1130)  */
        /*  1300-1300 1400-1400    */
        if (startIndex - 1 < 0) {
          return {
            ...item,
            beginIndex: '',
          };
        }
        return {
          ...item,
          endIndex: TIME_ENUM[startIndex - 1],
        };
      } else if (
        (endIndex >= item.beginIndex && endIndex < item.endIndex) ||
        (endIndex >= item.beginIndex && endIndex > item.endIndex && beginIndex < item.beginIndex)
      ) {
        // item.beginIndex > beginIndex && item.beginIndex <= endIndex
        /*   1200- (1300->1615)  (1600->1630)-1630  */
        if (endindex + 1 > TIME_ENUM.length - 1) {
          return {
            ...item,
            endIndex: '',
          };
        }
        return {
          ...item,
          beginIndex: TIME_ENUM[endindex + 1],
        };
      }
      return {
        ...item,
      };
    }
  });
  arr = arr.filter((item) => item);
  return arr;
};
/* 输入最大值最小值比例固定值 */
export const changeVal = (key, allVal, e, data) => {
  let reg = /^[0-9]*\.?[0-9]*$/;
  if (key === 'scale') {
    reg = /^[0-9]*$/;
  }
  if (!reg.test(e.target.value)) {
    return data;
  }
  let arr = [...data];
  arr = arr.map((item) => {
    if (item.id === allVal?.id) {
      return { ...item, [key]: e.target.value };
    }
    return { ...item };
  });
  return arr;
};
/* 失焦最大值最小值比例固定值 */
export const onBlurFn = (key, allVal, e, data, capacity) => {
  // renovate.isRenovate = true;
  let arr = [...data];
  arr = arr.map((item) => {
    if (item.id === allVal?.id) {
      let val = e.target.value;
      if (key == DATATYPE['MAXVALUE']) {
        if (e.target.value > Number(capacity)) {
          val = Number(capacity)?.toFixed(3);
        } else if (e.target.value < Number(allVal?.[DATATYPE['MINVALUE']])) {
          val = allVal?.[DATATYPE['MINVALUE']];
        } else {
          val = Number(val)?.toFixed(3);
        }
      } else if (
        key === DATATYPE['MINVALUE'] &&
        e.target.value > Number(allVal?.[DATATYPE['MAXVALUE']])
      ) {
        val = allVal?.[DATATYPE['MAXVALUE']];
      } else if (key === DATATYPE['FIXEDVALUE'] && e.target.value > Number(capacity)) {
        val = Number(capacity)?.toFixed(3);
      } else if (key !== DATATYPE['RATEVALUE']) {
        val = val === '-' ? '0.000' : Number(val)?.toFixed(3);
      } else if (key === DATATYPE['RATEVALUE']) {
        val = !val?.length ? 0 : Number(val)?.toFixed(0);
      }
      return {
        ...item,
        [key]: val,
      };
    }
    return { ...item };
  });
  return arr;
};
/* 添加调整比例 */
export const addFn = (data, capacity) => {
  let arr = [...data];
  arr.push({
    id: (arr?.[arr.length - 1]?._index || 0) + 1,
    beginIndex: '',
    endIndex: '',
    useType: 'dataSource',
    fixedValue: '0.000',
    maxValue: Number(capacity)?.toFixed(3) || '0.000',
    rateValue: 100,
    minValue: '0.000',
    _index: (arr?.[arr.length - 1]?._index || 0) + 1,
  });
  return arr;
};
/* 调整比例toolTip */
export const AdjustToolFn = (data) => {
  return (
    <div className={styles.adjustToolBox}>
      {data?.[0]?.axisValue}
      {data.map((item, index) => {
        return (
          <span key={index}>
            {item?.seriesName}:
            <font style={{ color: item?.color || '', margin: '5px' }}>{item?.value || '-'}</font>
          </span>
        );
      })}
    </div>
  );
};
/* init保存,计算数据 */
export const initComputeData = (data) => {
  let _data = data.map((item) => {
    let beginIndex = TIME_ENUM?.findIndex((val) => val == item?.beginIndex || '');
    let endIndex = TIME_ENUM?.findIndex((val) => val == item?.endIndex || '');
    let timeSlot = `${item?.beginIndex || ''}-${item?.endIndex || ''}`;
    return {
      ...item,
      beginIndex,
      endIndex,
      timeSlot,
    };
  });
  _data = _data.filter((item) => item.beginIndex != -1 || item.endIndex != -1);
  return _data;
};
/* 按钮权限 */
export const useBtnJurisdiction = (data) => {
  return data.filter((item) => item.menuCode === 'adjustRatioSave')?.length;
};
